import javax.swing.JToolBar;
import javax.swing.JButton;
import javax.swing.ImageIcon;
import javax.swing.border.SoftBevelBorder;
import java.net.URL;
import java.net.URLClassLoader;
import javax.swing.ButtonGroup;
import javax.swing.border.EtchedBorder;

/**
 * ToolBar represents main toolbar for the application.
 *
 * @version 1.0
 * @author P.Jaya Krishna
 */
class ToolBar extends JToolBar
{
	// refer to the buttons that are added to the toolbar
	JButton TBNew;
	JButton TBOpen;
	JButton TBSave;
	JButton TBRefresh;
	JButton TBUndo;
	JButton TBRedo;
	JButton TBStepByStep;
	JButton TBSelect;
	JButton TBVertex;
	JButton TBEdge;
	JButton TBShowWeights;
	JButton TBAdjMatrix;
	
	// refers to the frame to which this toolbar is attached
	GFrame gFrame;
	
	/**
	 * Creates a toolbar and its buttons for the frame "gFrame"
	 */
	public ToolBar(GFrame gFrame)
	{
		this.gFrame = gFrame;
		
		// create the buttons
		
		//URLClassLoader cl = (URLClassLoader)getClass().getClassLoader();
		
		TBNew           = new JButton(new ImageIcon
							("images/new.png"));
		TBOpen          = new JButton(new ImageIcon
							("images/open.gif"));
		TBSave          = new JButton(new ImageIcon
							("images/save.gif"));
		TBRefresh       = new JButton(new ImageIcon
							("images/refresh.png"));
		TBUndo          = new JButton(new ImageIcon
							("images/undo.gif"));
		TBRedo          = new JButton(new ImageIcon
							("images/redo.gif"));
		TBStepByStep	= new JButton("Graph Animation");
		TBSelect        = new JButton("Sel");
		TBVertex        = new JButton("Vertex");
		TBEdge          = new JButton("Edge");
		TBShowWeights   = new JButton("ShowWeights");
		TBAdjMatrix     = new JButton("AdjMatrix");
		
		// set tool tip for the buttons
		TBNew.setToolTipText("New File");
		TBOpen.setToolTipText("Open");
		TBSave.setToolTipText("Save");
		TBRefresh.setToolTipText("Refresh");
		TBUndo.setToolTipText("Undo");
		TBRedo.setToolTipText("Redo");
		TBStepByStep.setToolTipText("Step By Step Graph Operations");
		TBSelect.setToolTipText("Selection");
		TBVertex.setToolTipText("Add/Remove Vertex");
		TBEdge.setToolTipText("Add/Remove Edge");
		TBShowWeights.setToolTipText("Show Edge Weights");
		TBAdjMatrix.setToolTipText("Adjacency Matrix");
		
		// add the buttons to the toolbar
		add(TBNew);
		addSeparator();
		add(TBOpen);
		add(TBSave);
		addSeparator();
		add(TBRefresh);
		addSeparator();
		add(TBUndo);
		add(TBRedo);
		addSeparator();
		add(TBStepByStep);
		addSeparator();
		add(TBSelect);
		add(TBVertex);
		add(TBEdge);
		addSeparator();
		add(TBShowWeights);
		add(TBAdjMatrix);
		
		// toolbar should not be dockable
		setFloatable(false);
		setBorder(new EtchedBorder());
		
		// create and add handler to the toolbar
		ToolBarHandler tbHandler = new ToolBarHandler(gFrame);
		
		TBNew.addActionListener(tbHandler);
		TBOpen.addActionListener(tbHandler);
		TBSave.addActionListener(tbHandler);
		TBRefresh.addActionListener(tbHandler);
		TBUndo.addActionListener(tbHandler);
		TBRedo.addActionListener(tbHandler);
		TBStepByStep.addActionListener(tbHandler);
		TBSelect.addActionListener(tbHandler);
		TBVertex.addActionListener(tbHandler);
		TBEdge.addActionListener(tbHandler);
		TBShowWeights.addActionListener(tbHandler);
		TBAdjMatrix.addActionListener(tbHandler);
		
		TBSelect.setSelected(true);
		TBShowWeights.setSelected(true);
		TBStepByStep.setSelected(true);
		TBStepByStep.setBorder(new SoftBevelBorder(SoftBevelBorder.LOWERED));
		TBSelect.setBorder(new SoftBevelBorder(SoftBevelBorder.LOWERED));
		TBShowWeights.setBorder(new SoftBevelBorder(SoftBevelBorder.LOWERED));
		TBUndo.setEnabled(false);
		TBRedo.setEnabled(false);
	}
	
	public void enable()
	{
		TBNew.setEnabled(true);
		TBOpen.setEnabled(true);
		TBSave.setEnabled(true);
		TBRefresh.setEnabled(true);
		TBUndo.setEnabled(true);
		TBRedo.setEnabled(true);
		TBStepByStep.setEnabled(true);
		TBSelect.setEnabled(true);
		TBVertex.setEnabled(true);
		TBEdge.setEnabled(true);
		TBShowWeights.setEnabled(true);
		TBAdjMatrix.setEnabled(true);

	}
	
	public void disable()
	{
		TBNew.setEnabled(false);
		TBOpen.setEnabled(false);
		TBSave.setEnabled(false);
		TBRefresh.setEnabled(false);
		TBUndo.setEnabled(false);
		TBRedo.setEnabled(false);
		TBStepByStep.setEnabled(false);
		TBSelect.setEnabled(false);
		TBVertex.setEnabled(false);
		TBEdge.setEnabled(false);
		TBShowWeights.setEnabled(false);
		TBAdjMatrix.setEnabled(false);
	}
}